package net.yla.smartbus.module.ba.right.account.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.vip.vjtools.vjkit.collection.ListUtil;
import net.yla.smartbus.core.util.IPUtil;
import net.yla.smartbus.core.util.PojoJsonUtil;
import net.yla.smartbus.module.ba.right.admin.entity.AdminLoginTracesDO;
import net.yla.smartbus.module.ba.right.admin.query.IPCount;
import net.yla.smartbus.module.ba.right.admin.query.IPQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import net.yla.smartbus.module.ba.right.account.mapper.LoginTracesMapper;
import net.yla.smartbus.module.ba.right.account.entity.LoginTracesDO;
import net.yla.smartbus.module.ba.right.account.service.LoginTracesService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2020-08-29 11:14
 * @Author: Being Nobody
 * @Version: 1.0
 */
@Service
public class LoginTracesServiceImpl implements LoginTracesService {

    @Resource
    private LoginTracesMapper loginTracesMapper;

    @Override
    public int deleteByPrimaryKey(Integer traceId) {
        return loginTracesMapper.deleteByPrimaryKey(traceId);
    }

    @Override
    public int insert(LoginTracesDO record) {
        return loginTracesMapper.insert(record);
    }

    @Override
    public int insertSelective(LoginTracesDO record) {
        return loginTracesMapper.insertSelective(record);
    }

    @Override
    public LoginTracesDO selectByPrimaryKey(Integer traceId) {
        return loginTracesMapper.selectByPrimaryKey(traceId);
    }

    @Override
    public int updateByPrimaryKeySelective(LoginTracesDO record) {
        return loginTracesMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(LoginTracesDO record) {
        return loginTracesMapper.updateByPrimaryKey(record);
    }

    @Override
    public LoginTracesDO selectByLoginIP(long ipL) {
        return loginTracesMapper.selectByLoginIP(ipL);
    }

    @Override
    public int ipOfCount(IPCount count) {
        long ip = IPUtil.ipv4ToLong(count.getIp());
        return loginTracesMapper.ipOfCount(count, ip);
    }

    @Override
    public List<JSONObject> ipOfQuery(IPQuery query) {
        // 校验
        // 查询
        long ip = 0;
        if (StringUtils.isNotEmpty(query.getIp())) {
            ip = IPUtil.ipv4ToLong(query.getIp());
        }
        List<LoginTracesDO> tracesDOList = loginTracesMapper.ipOfQuery(query, ip);
        // 处理
        List<JSONObject> result = this.disposeOfIP(tracesDOList, query.getFields());
        return result;
    }

    @Override
    public JSONObject ipOfView(Integer traceId) {
        LoginTracesDO tracesDO = loginTracesMapper.selectByPrimaryKey(traceId);
        JSONObject object = null;
        if (tracesDO != null) {
            object = new JSONObject();
            object.put("id", tracesDO.getTraceId());
            object.put("ip", IPUtil.longToIpv4(tracesDO.getIp()));
            object.put("errorCount", tracesDO.getErrorCount());
            object.put("errorTime", tracesDO.getLastErrorTime());
        }

        return object;
    }

    private List<JSONObject> disposeOfIP(List<LoginTracesDO> tracesDOList, List<String> fields) {
        List<JSONObject> list = null;
        if (ListUtil.isNotEmpty(tracesDOList) && tracesDOList.size() > 0) {
            list = new ArrayList<>(tracesDOList.size());
            for (LoginTracesDO tracesDO : tracesDOList) {
                // id ip 错误次数 错误时间
                JSONObject object = PojoJsonUtil.writeJsonByField(tracesDO, fields);
                list.add(object);
            }
        }
        return list;
    }
}
